import { IExecuteFunctions } from 'n8n-core';
import {
        INodeExecutionData,
        INodeType,
        INodeTypeDescription,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';


export class Pushover implements INodeType {
        description: INodeTypeDescription = {
                displayName: 'Pushover',
                name: 'pushover',
                group: ['transform'],
                version: 1,
                description: 'node send notification by pushover',
                defaults: {
                        name: 'Pushover',
                        color: '#772244',
                },
                inputs: ['main'],
                outputs: ['main'],
                properties: [
                        {
                                displayName: 'User Key',
                                name: 'userKey',
                                type: 'string',
                                default: '',
                                placeholder: '',
                                description: 'The description text',
                        },
                        {
                                displayName: 'APP Token',
                                name: 'appToken',
                                type: 'string',
                                default: '',
                                placeholder: 'app token',
                                description: 'The description text',
                        }
                ]
        };


        async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
                const apiUrl = "https://api.pushover.net/1/messages.json";
                const items = this.getInputData();

                const options: OptionsWithUri = {
                        method: 'POST',
                        json: true,
                        url: apiUrl,
                        formData: {
                                user: this.getNodeParameter('userKey', 0, ''),
                                token: this.getNodeParameter('appToken', 0, ''),
                                title: items['title']? items['title']: 0,
                                message: items['content']? items['content']: 0
                        },
                };
                let response = await this.helpers.request(options);
                return this.prepareOutputData(response);
        }
}
